layui.use(['table','layer','form','laydate'], function() {
	var table = layui.table;
	var layer = layui.layer;
	var form = layui.form;
	var laydate = layui.laydate;
	var $ = layui.jquery;	
	const url = location.search; //获取url
	var arr=url.split('=');
	var storeId=arr[1];
	var keyword=''
	
	//console.log(storeId);
	//新增弹窗中的保存添加事件
		/*$("body").on("click","#saveBtn",function(){	//二次选择
			//获取form的全部数据
			var data = form.val("storeProductForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/storeProduct/add",data,function(){
				layer.closeAll();
				$("#searchBtn").click();
			});
		});*/
	
	
	//给新增按钮绑定事件
	$("#addBtn").on("click",function(){		
		//弹出窗口
		$.get("/product/getList",function(tops){
		//console.log(tops);
		layer.open({
			type:1
			,area:['400px','500px']//窗口大小
			,title:"新增商品"
			,content:$("#addStoreProductTemplate").html()
		});
		//传top
		$.each(tops,function(index,value){
			$("#parentId").append("<option value='"+value.id+"' >"+value.name+"</option>")
			});
					
		form.render();
		
		/*laydate.render({
			elem:'#birthday'//指定元素
		});*/
		
		//新增弹窗中的保存添加事件
		$("#saveBtn").on("click",function(){	
			//获取form的全部数据
			var data = form.val("storeProductForm");
		
			console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/storeProduct/add",{
							storeId:storeId,
							productId:data.parent_id,
							num:parseInt(data.num)},function(){
				layer.closeAll();
				$("#searchBtn").click();

			});
		});
		
	});
	});
	
	
	//给查询按钮绑定事件
	$("#searchBtn").on("click",function(){
		table.render({
			elem: '#storeProductTable'
			, url: '/storeProduct/list?storeId='+storeId //数据接口
			, page: true //开启分页
			, cols: [[ //表头
				{ field: 'id', title: 'ID', width: '24%', fixed: 'left' }
				, { field: 'productName', title: '商品名', width: '25%' }
				, { field: 'num', title: '商品数量', width: '25%'}
				, { title: '操作', width: '25%' ,toolbar:"#operateTemplate"}
			]],
			limits:[5,10,15,20],
			limit:5,
			where:{"keyword":$("#searchStoreProductname").val()}
		});
		updateChangeList()
		table.on('tool(storeProductTable1)',function(obj){
			
			var storeProductid=obj.data.id;//获取当前操作行的id
			console.log(obj.data);
			if(obj.event == "del" ){//点击删除按钮
				layer.confirm("你确定要删除该么？",function(){
					$.post("/storeProduct/del",{
							storeProductId:obj.data.id,
							storeId:obj.data.store_id,
							productId:obj.data.product_id,
							num:parseInt(obj.data.num)
							
						},function(){
						layer.closeAll();
						$("#searchBtn").click();
					});
				}/*,function(){}*/);
			}else if(obj.event == "in"||obj.event == "out"){//点击编辑按钮
				//$.get("/storeProduct/"+storeProductid,function(u){
					
					layer.open({
						type:1
						,area:['400px','500px']//窗口大小
						,title:"编辑用户"
						,content:$("#inStoreProductTemplate").html()
					});
					
					$("#productName").val(obj.data.productName)
					$("#num").val(obj.data.num)
					
					
					
					//第一个获取值，第二个赋值给u
					console.log(form.val("addstoreProductForm"));
					$("#saveBtn").on("click",function(){	
						var addnum=$("#addnum").val();
						//获取form的全部数据
						//var data = form.val("storeProductForm");
						//console.log(data);
						//把数据推送道后台
						//ajax
						$.post("/storeProduct/save",{
							storeProductId:obj.data.id,
							storeId:obj.data.store_id,
							productId:obj.data.product_id,
							num:parseInt(obj.data.num)+parseInt(addnum),
							changeNum:addnum
						},function(){
							layer.closeAll();
							$("#searchBtn").click();
						});
					});
					
				//});
			}else{
				$.get("/role/all",{storeProductId:storeProductid},function(roles){
						
						layer.open({
							type:1
							,title:'绑定角色'
							,area:['400px',"200px"]
							,content:$("#storeProductRoleTemplate").html()
						});
						$.each(roles,function(index,value){
							$("#storeProductRoleDiv").append("<input type='checkbox' "+(value.selected?'checked':'')+" value='"+value.id+"' title='"+value.name+"'>");
						});
						form.render();
						form.on('checkbox',function(data){
							//console.log(data);
							var roleId = data.value;
							//storeProductid
							$.post("/storeProductRole/operate",{storeProductId:storeProductid,roleId:roleId,type:data.elem.checked?'add':'cancel'},function(){

							});
						});
				});
				
			}
			
		});
		
	}).click();

	updateChangeList()

	function updateChangeList() {
		table.render({
			elem: '#storeRTable'
			, url: '/storeProduct/changeList?storeId=' + storeId //数据接口
			, page: true //开启分页
			, cols: [[ //表头
				{field: 'id', title: 'ID', width: '20%', fixed: 'left'}
				, {field: 'productName', title: '商品名', width: '20%'}
				, {field: 'num', title: '商品数量', width: '20%'}
				, {field: 'type', title: '更新类型', width: '20%'}
				, {field: 'date', title: '操作时间', width: '20%'}

			]],
			limits: [5, 10, 15, 20],
			limit: 10,
			where:{"keyword":$("#searchStoreProductname").val()}
		});
	}
});